﻿using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using RiskMan.Config;
using RiskMan.DataClass.Models.Plaza_II;
using RiskMan.Views.Dialogs;

namespace RiskMan.Views.Plaza_II
{
    /// <summary>
    /// Interaction logic for PlazaIIView.xaml
    /// </summary>
    public partial class PlazaIIView
    {
        private readonly DispatcherTimer _dsTimer = new DispatcherTimer();
        public PlazaIIView()
        {
            InitializeComponent();

            DtgdPlazaRobot.ItemsSource = MainWindow.StaticLevel._dataProvider.RobotItemPlazaIIlist;
        }

        private void Button_AddRow_Click(object sender, RoutedEventArgs e)
        {
            RobotItemPlazaII rpl = new RobotItemPlazaII();

            AddNewRobotPlazaII add = new AddNewRobotPlazaII(rpl);
            add.ShowDialog();

            if (add.DialogResult == true)
            {
                MainWindow.StaticLevel._dataProvider.RobotItemPlazaIIlist.Add(rpl);
                Cfg.SetLog("Добавлены новые условия", MainWindow.StaticLevel.ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (DtgdPlazaRobot.SelectedItems.Count == 0)
                new DialogMessage("Не выбрано ни одного элемента для удаления", "Внимание!");

            if (DtgdPlazaRobot.SelectedItems.Count == 1)
            {
                int index = DtgdPlazaRobot.SelectedIndex + 1;
                try
                {
                    if (new DialogOkCancel("Вы действительно хотите удалить строку " + index + "?", "Внимание").Result == MessageBoxResult.OK)
                    {
                        foreach (var item in MainWindow.StaticLevel._dataProvider.RobotItemPlazaIIlist)
                        {
                            if (item.GuidItem == ((RobotItemPlazaII)(DtgdPlazaRobot.SelectedItem)).GuidItem)
                            {
                                MainWindow.StaticLevel._dataProvider.RobotItemPlazaIIlist.Remove(item);
                                break;
                            }
                        }

                    }
                }
                catch
                { }
            }
        }

        public void _dsTimer_Tick(Object sender, EventArgs args)
        {
            //if (MainWindow.StaticLevel.ConnectingOrDisconnecting())
            //{
                if (MainWindow.StaticLevel._dataProvider.RobotItemPlazaIIlist.Count > 0)
                {
                    foreach (var item in MainWindow.StaticLevel._dataProvider.RobotItemPlazaIIlist)
                    {
                        if (item.IsRun)
                        {
                            LogikPlazaII logik = new LogikPlazaII();
                            logik.Start(item);
                        }
                    }

                }
            //}
        }


        private void BtnCopy_Click(object sender, RoutedEventArgs e)
        {

        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            if (MainWindow.StaticLevel._dataProvider.RobotItemPlazaIIlist.Count > 0)
            {
                foreach (var item in MainWindow.StaticLevel._dataProvider.RobotItemPlazaIIlist)
                {
                    if (item.Check)
                        item.IsRun = true;
                }

            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            if (MainWindow.StaticLevel._dataProvider.RobotItemPlazaIIlist.Count > 0)
            {
                foreach (var item in MainWindow.StaticLevel._dataProvider.RobotItemPlazaIIlist)
                {
                    if (item.Check)
                        item.IsRun = false;
                }
            }
        }


        private void DtgdPlazaRobot_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (DtgdPlazaRobot.SelectedIndex >= 0)
            {
                RobotItemPlazaII rpl = ((DataGrid)sender).SelectedItem as RobotItemPlazaII;
                AddNewRobotPlazaII addNewRobot = new AddNewRobotPlazaII(rpl);
                addNewRobot.ShowDialog();
            }
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            if (MainWindow.StaticLevel._dataProvider.RobotItemPlazaIIlist.Count > 0)
            {
                foreach (var item in MainWindow.StaticLevel._dataProvider.RobotItemPlazaIIlist)
                {
                    var isChecked = ((CheckBox)sender).IsChecked;
                    if (isChecked != null) item.Check = (bool)isChecked;
                }
            }
        }

        private void PlazaIIView_OnLoaded(object sender, RoutedEventArgs e)
        {
            _dsTimer.Interval = TimeSpan.FromMilliseconds(1000);
            _dsTimer.Start();
            _dsTimer.Tick += new EventHandler(_dsTimer_Tick);
        }
    }
}
